import sys
from PyQt5.QtWidgets import QDialog, QVBoxLayout, QLabel, QLineEdit, QPushButton, QApplication

from PyQt5.QtWidgets import QMainWindow
from qt_learn.ui import Ui_mainWindow

class LoginDialog(QDialog):
    def __init__(self):
        super().__init__()

        # 设置居中的主布局
        main_layout = QVBoxLayout()

        # 添加标签
        label = QLabel("请登录：")
        main_layout.addWidget(label)

        # 用户名输入框
        self.username_input = QLineEdit()
        self.username_input.setPlaceholderText("请输入用户名：")
        main_layout.addWidget(self.username_input)

        # 密码输入框
        self.password_input = QLineEdit()
        self.password_input.setPlaceholderText("请输入密码：")
        self.password_input.setEchoMode(QLineEdit.Password)
        main_layout.addWidget(self.password_input)

        # 登录按钮
        login_button = QPushButton("登录")
        login_button.clicked.connect(self.check_credentials)
        main_layout.addWidget(login_button)

        # 设置窗口布局
        self.setLayout(main_layout)

    def check_credentials(self):
        username = self.username_input.text()
        password = self.password_input.text()

        # 检查用户名和密码
        if username == "admin" and password == "password":
            print("Login successful!")
            self.accept()  # 关闭登录对话框
        else:
            print("Incorrect username or password.")


class MyWindow(QMainWindow, Ui_mainWindow):

    def __init__(self):
        super(MyWindow, self).__init__()
        self.setupUi(self)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    myWin = MyWindow()
    # 显示登录对话框
    login_dialog = LoginDialog()
    if login_dialog.exec_() == QDialog.Accepted:
        # 如果登录成功，则显示主窗口
        myWin.show()

    sys.exit(app.exec_())